package socket;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import service.Message;
import service.MessageQueue;
import service.MessageHandler;

public class ChatChannel {
        private Map<String, MessageHandler> usersMap = new HashMap<String, MessageHandler>(); 
        //[ek] TODO remove dead comments
        private MessageQueue messageQueue = new MessageQueue();
        private MessageSender messageSender;
        private String name;
        
        public ChatChannel(String name){
                messageSender = new MessageSender();
                this.name = name;
                messageSender.start();
        }
        
        public String getName() {
                return name;
        }
        
        public Map<String, MessageHandler> getUsersMap() {
                return usersMap;
        }
        class MessageSender extends Thread {
            
            @Override
            public void run() {
                    Message message;
                    String reciever;
                    while(!isInterrupted()){
                    	
                            message = messageQueue.getNextMessage();
                            if (message == null){
                            	System.out.println("exited on break");
                                    break;
                            }
                            
                            //[ek] TODO why else? you have break
                            reciever = message.getReciever(); 
                            if (reciever.equals("all"))
                                    for (String key: usersMap.keySet())
                                            try {
                                                    usersMap.get(key).sendMessage(message.getMessage());
                                            } catch (IOException e) {
                                                    System.out.println("User " + key + " is offline");
                                            }
                            else
                                    try {
                                            usersMap.get(reciever).sendMessage("private from " + message.getMessage());
                                    } catch (IOException e) {
                                            System.out.println("User " + reciever + " is offline");
                                    }
                    }
            }
    }
    
    public synchronized void addUser(MessageHandler messageHandler, String login){
            usersMap.put(login, messageHandler);
            System.out.println("user " + login + " added to " + this.name + " channel");
    }
    
    public synchronized void removeUser(String login){
            usersMap.remove(login);
    }
    
    public MessageQueue getMessageQueue(){
            return messageQueue;
    }
    
    public void close(){
            messageQueue.stop();
            messageSender.interrupt();
    }
    
    public synchronized boolean isInChannel(String login){
            return usersMap.containsKey(login);
    }

    public static ChatChannel getByName (String name) {
            for (ChatChannel channel: ChatServer.channels) 
                    if (name.equals(channel.getName()))
                            return channel;
                            
            return null;
    }
    
}